<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生名称" prop="studentName" v-if="!isStudentRole">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="zjbgShStatus">
        <el-select v-model="queryParams.zjbgShStatus" placeholder="请选择审核状态" filterable clearable>
          <el-option
            v-for="dict in dict.type.sxgc_shzt"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="导师名称" prop="teacherName"  v-if="!isStudentRole">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入导师名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sxcg:zjbg:add']"
        >上传</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sxcg:zjbg:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sxcg:zjbg:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="zjbgList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="50" type="index" />
      <el-table-column label="学生" align="center" prop="studentName" />
      <el-table-column label="总结标题" align="center" prop="title" />
      <el-table-column label="提交时间" align="center" prop="tjsj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.tjsj, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="导师" align="center" prop="teacherName" />
      <el-table-column label="审核状态" align="center" prop="zjbgShStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sxgc_shzt" :value="scope.row.zjbgShStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="shsj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.shsj, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.zjbgShStatus === 'xxg'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sxcg:zjbg:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.zjbgShStatus === 'dsh' && !isStudentRole"
            size="mini"
            type="text"
            icon="el-icon-s-check"
            @click="handleReview(scope.row)"
            v-hasPermi="['sxcg:zjbg:review']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sxcg:zjbg:remove']"
          >删除</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              v-hasPermi="['sxcg:zjbg:view']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改总结报告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px" inline>
        <el-form-item label="学生" prop="studentId">
          <el-select v-model="form.studentId" placeholder="请选择学生" filterable clearable disabled style="width: 240px;">
            <el-option
              v-for="item in studentSelectData"
              :key="item.studentId"
              :label="item.name"
              :value="item.studentId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="导师" prop="teacherId">
          <el-select v-model="form.teacherId" placeholder="请选择导师" filterable clearable disabled style="width: 240px;">
            <el-option
              v-for="item in teacherSelectData"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            >
              <span style="float: left">{{ item.nickName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phoneNumber }}</span></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="总结标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入总结标题"  style="width: 600px;"/>
        </el-form-item>
        <el-form-item label="总结内容" prop="content">
          <el-input v-model="form.content" type="textarea" rows="7" placeholder="请输入内容"  style="width: 600px;"/>
        </el-form-item>
        <el-form-item label="总结附件" prop="fj">
          <file-upload v-model="form.fj" :limit="10" style="width: 600px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--学生编辑弹出框-->
    <studentEdit v-model="currentEditZjbgId"   ref="studentEdit" ></studentEdit>
    <!--老师审核弹出框-->
    <teacherReview v-model="currentReviewZjbgId"  ref="teacherReview" ></teacherReview>
    <!--查看弹出框-->
    <viewInfo v-model="currentViewZjbgId"  ref="viewInfo" ></viewInfo>

  </div>
</template>

<script>
import { listZjbg, delZjbg, addZjbg, updateZjbg } from "@/api/sxcg/zjbg"
import {getUserSelect} from "@/api/system/user";
import {getStudentSelect, getUserInfoData} from "@/api/xxgl/xsxx";
import teacherReview from "@/views/sxcg/zjbg/teacherReview.vue";
import studentEdit from "@/views/sxcg/zjbg/studentEdit.vue";
import viewInfo from "@/views/sxcg/zjbg/viewInfo.vue";

export default {
  name: "Zjbg",
  components: {viewInfo, studentEdit, teacherReview},
  dicts: ['sys_user_sex', 'sxgc_shzt', 'sys_show_hide'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 总结报告表格数据
      zjbgList: [],
      // 导师下拉框数据
      teacherSelectData: [],
      // 学生下拉框数据
      studentSelectData: [],
      // 当前编辑的总结报告ID
      currentEditZjbgId: null,
      // 当前审核的总结报告ID
      currentReviewZjbgId: null,
      // 当前查看的总结报告ID
      currentViewZjbgId: null,
      // 当前用户ID
      currentUserId: this.$store.state.user.id,
      // 当前用户角色
      currentUserRole: this.$store.state.user.roles[0],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentId: null,
        studentName: null,
        zjbgShStatus: null,
        teacherId: null,
        teacherName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        studentId: [{ required: true, message: "请选择学生", trigger: "blur" }],
        teacherId: [{ required: true, message: "请选择导师", trigger: "blur" }],
        title: [{ required: true, message: "请输入总结标题", trigger: "blur" }],
        content: [{ required: true, message: "请输入总结内容", trigger: "blur" }],
        tjsj: [{ required: true, message: "请选择提交时间", trigger: "blur" }],
        zjbgShStatus: [{ required: true, message: "请选择审核状态", trigger: "blur" }],
        shsj: [{ required: true, message: "请选择审核时间", trigger: "blur" }],
        shyj: [{ required: true, message: "请输入审核意见", trigger: "blur" }],
        fj: [{ required: true, message: "请上传总结附件", trigger: "blur" }],
      }
    }
  },
  created() {
    this.getList()
    this.getUserSelectData()
  },
  computed: {
    // 判断当前是否为学生角色
    isStudentRole() {
      return this.currentUserRole === 'student';
    },
  },
  methods: {
    /** 查询总结报告列表 */
    getList() {
      this.loading = true
      if(this.currentUserRole === 'student'){
        this.queryParams.glId = this.currentUserId
      }else if(this.currentUserRole === 'teacher'){
        this.queryParams.teacherId = this.currentUserId
      }
      listZjbg(this.queryParams).then(response => {
        this.zjbgList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        zjbgId: null,
        studentId: null,
        title: null,
        content: null,
        fj: null,
        tjsj: null,
        zjbgShStatus: null,
        teacherId: null,
        shsj: null,
        shyj: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.zjbgId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查询用户下拉框数据 */
    getUserSelectData() {
      /*  roleId  2:学生  3:老师 */
      /* 查询角色为老师下拉框内容*/
      getUserSelect(3).then(response => {
        this.teacherSelectData = response.data
      })
      /* 查询学生信息表 学生下拉框内容*/
      getStudentSelect().then(response => {
        this.studentSelectData = response.data
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      /* 根据当前登录用户查询个人信息*/
      getUserInfoData(this.currentUserId).then(res => {
        this.form.studentId = res.data.studentId
        if(res.data.teacherId !== null){
          this.form.teacherId = res.data.teacherId
        }else{
          this.$modal.msgWarning("实习信息中尚未绑定导师，请先完善信息！")
          return;
        }
        if(res.data.sxStatus === "wks"){
          this.$modal.msgWarning("当前实习状态为 【未开始】，不能提交总结报告！")
          return;
        }
        if(res.data.sxStatus === "yjs"){
          this.$modal.msgWarning("当前实习状态为 【已结束】，不能提交总结报告！")
          return;
        }
        if(res.data.zjbgId !== null){
          this.$modal.msgWarning("您已提交总结报告，无需重复提交！")
          return;
        }
        this.open = true
      });
      this.title = "添加"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.currentEditZjbgId = row.zjbgId
    },
    /** 审核按钮操作 */
    handleReview(row){
      this.currentReviewZjbgId = row.zjbgId
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.currentViewZjbgId = row.zjbgId
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.zjbgId != null) {
            this.form.shsj = this.$dayjs().format('YYYY-MM-DD HH:mm:ss')
            updateZjbg(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            this.form.tjsj = this.$dayjs().format('YYYY-MM-DD HH:mm:ss')
            this.form.zjbgShStatus = "dsh"
            addZjbg(this.form).then(response => {
              this.$modal.msgSuccess("上传成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const zjbgIds = row.zjbgId || this.ids
      this.$modal.confirm('是否确认删除?').then(function() {
        return delZjbg(zjbgIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sxcg/zjbg/export', {
        ...this.queryParams
      }, `总结报告${this.$dayjs().format('YYYY-MM-DD HH:mm:ss')}.xlsx`)
    }
  }
}
</script>
